<?php include_once("../config.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php include_once(ROOT_PATH_.'/user/modules/head.php'); ?>
<?php include_once(ROOT_PATH_.'/user/modules/mobile.php'); ?>
<style>
    body{ background-color: #ffffff}
    #checkin #content{ margin-left: 0px;}
    #checkin .layui-input-block{ margin-left: 0px;}
    #checkin .layui-form-item .layui-form-label{width: 50px;}
    #checkin .layui-form-item .layui-input-block{ margin-left: 85px;}
    #modify-form{ padding: 0 10px;}
</style>



<?
    //由于经过验证权限之后才继续插入数据的话，可能会导致记录丢失。现在调整，思路如下
    //先插入数据，并且带上nid。
    //验证权限，没问题则改变数据状态。有问题则标记不显示。
    //同时，在扫描结果中显示，此次扫描检查不通过，是否人工调整
?>





<?

$equipment=$_REQUEST['eid'];
$openid=$_REQUEST['openid'];

//获取设备名称
$detail=select_one_line_all_items($table_Prefix."equipment","nid = '".$equipment."'");



//20180228，不论如何，先插入签到数据。不合法再处理状态
//如果有openid的话，就不用再插了，因为表示这个已经跳转一次。
//如果有openid的话，就不用再插了，因为表示这个已经跳转一次。
//如果有openid的话，就不用再插了，因为表示这个已经跳转一次。


if(!$openid){
$_REQUEST['ai_authur']=$user['nid'];//需要先获取openid
$_REQUEST['ai_name']=$detail['name'];
$_REQUEST['ai_applydate']=date("Y-m-d H:i:s");
$_REQUEST['ai_status']="未确认";
$_REQUEST['ai_equipmentid']=$equipment;




$sql=insert_date($table_Prefix."access_record",$_REQUEST);
$query = mysql_query($sql) or die ($sql);
mysql_close($con);

}










if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') == true) {

    if(!$openid)
    {
        header('Location:http://wx.enpingshi.com/var_checkin.php?eid='.$equipment);
    }


}


//用于查询员工的微信openid
$employee=select_one_line_all_items($table_Prefix."employee","nid = '".$detail['masterid']."'");
//用于查询负责签到的员工的微信openid
$user=select_one_line_all_items($table_Prefix."employee","wechat_openid = '".$openid."'");


//判断这辆车符合间隔否
//读取这辆车的最新进出记录
$record=select_lines_all_items($table_Prefix."access_record","nid desc","equipmentid = '".$equipment."'","10");



$period="300";




$quanxian=explode(",", $user['quanxian']);







//判断有没有车，判断扫码的人是不是合法，判断这辆车的签到间隔
if(($detail['nid'])&&($user["wechat_openid"])&&( in_array("access_record",$quanxian)))
{





    $access_times=count_by_item($table_Prefix."access_record","equipmentid = '".$equipment."' and applydate like '%".date("Y-m-d")."%' ");
    $access_times= $access_times+1;

    $nowtime=strtotime(date("Y-m-d H:i:s"));
    $lasttime=strtotime($record[1]['applydate'])+$period;


    if($lasttime>$nowtime)
    {
        $record_status="异常";
    }
    else
    {
        //20180228原本检查无误之后就可以插入数据
        //20180228原本检查无误之后就可以插入数据
        //20180228原本检查无误之后就可以插入数据
        //20180228原本检查无误之后就可以插入数据
//        //echo "正常，可以签到";
//        $_REQUEST['ai_authur']=$user['nid'];
//        $_REQUEST['ai_name']=$detail['name'];
//        $_REQUEST['ai_applydate']=date("Y-m-d H:i:s");
//        $_REQUEST['ai_status']="正常";
//        $_REQUEST['ai_equipmentid']=$equipment;
//        $_REQUEST['ai_payment']=$detail['payment'];
//
//
//        $sql=insert_date($table_Prefix."access_record",$_REQUEST);
//        $query = mysql_query($sql) or die ($sql);
//        mysql_close($con);




        //20180228确认这些都没有异常，原本插入的数据，现在变成更新数据状态为正常即可
        //拿出这台车的最新的记录，然后用于修改他的状态

        $record_1=select_lines_all_items($table_Prefix."access_record","nid desc","equipmentid = '".$equipment."'",1);

        $updata['ai_status']="正常";

        $updata_sql=update_date($table_Prefix."access_record",$updata,"nid",$record_1[0]['nid']);
        $query = mysql_query($updata_sql) or die ($updata_sql);
        mysql_close($con);







        //发送签到通知

        $url = "http://wx.enpingshi.com/var_send_msg.php?openid=".$employee['wechat_openid']."&atimes=".$access_times;

        $send_msg=file_get_contents($url);



    }





}


else
{
?>
    <script type="text/javascript">
        alert("无签到权限或未找到该车辆，请重新签到。");
    </script>
<?}


?>
</head>

<body id="checkin">

<div id="topbar"><?php include ROOT_PATH_.'/user/modules/topbar.php'; ?></div>

<div id="container">

    <div id="maincontent">

        <div id="content">




            <div class="box layui-form">
                <h1>新增签到信息</h1>

                <?
                if($record_status=="异常")
                {
                    $notice="<span style='color:red'>签到异常，车辆签到间隔不足5分钟。</span>";
                }
                elseif((!$equipment)or(!$openid)or(!$user["wechat_openid"])or(!in_array("access_record",$quanxian)) )
                {
                    $notice="<span style='color:red'>签到失败，无签到权限或未找到该车辆，请重新签到。</span>";
                }
                else{
                    $notice="签到成功，当前签到信息如下。";
                }
                ?>


<div class="tips"><? echo $notice?></div>






<form id="modify-form" method="post" action="modify-access-record-function.php?nav-itemed=" enctype="multipart/form-data" class="layui-form">


                    <? $chepai=select_one_line_all_items($table_Prefix."equipment","nid = '".$_REQUEST['equipmentid']."'")?>




                    <div class="layui-form-item">
                        <label class="layui-form-label">车牌</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" class="layui-input" value="<?echo $detail['name']?>">
                        </div>
                    </div>



                    <div class="layui-form-item">
                        <label class="layui-form-label">日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="applydate" id="date" autocomplete="off" class="layui-input"  value="<?echo date('Y-m-d h:i:s')?>" onclick="laydate({istime: true, format: 'YYYY-MM-DD hh:mm:ss'})" class="laydate-icon">
                        </div>
                    </div>







<style>
.access_list{ margin: 25px 0 0 0;}
.access_list h2{ font-size: 18px; margin-bottom: 5px;}
.access_list ul{ list-style-position:outside;list-style-type: disc}
.access_list li{ margin: 8px 0 0 15px; padding-left: 15px;}
</style>






<div class="access_list">
<h2><b>最近10次签到记录</b></h2>

    <blockquote class="layui-elem-quote">
        若有数据状态异常，点击按钮重新确认<br/>
        异常状态的签到记录，不记录进统计内
    </blockquote>


    <table class="layui-table" lay-even="" lay-skin="nob">
        <colgroup>
            <col width="150">
        </colgroup>
        <thead>

        </thead>
        <tbody>
        <? for($i=1;$i<10;$i++){?>
        <tr>
            <td><b><? echo $i?></b>:<? echo $record[$i]['applydate']?>




<? if($record[$i]['status']!="正常"){?>
<span style="float: right"
class="layui-btn layui-btn-small layui-btn-radius layui-btn-danger"
id="btn<?echo $record[$i]['nid']?>"
data-status="<?echo $record[$i]['status']?>"
onclick="javascript:change_status(this)" >
异常</span>
                <?}else{?>
<span style="float: right;background-color: #009688!important" class="layui-btn layui-btn-small layui-btn-radius " id="status" >正常</span>
<?}?>


            </td>
        </tr>
            <?;}?>
        </tbody>
    </table>




</div>





<!--    <div class="layui-form-item" style="display: none">-->
<!--        <label class="layui-form-label">状态</label>-->
<!---->
<!--        <div class="layui-input-block">-->
<!--            <input type="radio" name="status" value="签到" title="签到" --><?// if($record_status!="异常"){echo "checked";} ?><!-->
<!--            <input type="radio" name="status" value="异常" title="异常" --><?// if($record_status=="异常"){echo "checked";} ?><!-->
<!--        </div>-->


    </div>




    <script>

        function change_status(e) {

            var id=e.id;
            var status=e.getAttribute("data-status");



                $.ajax({
                    type: "POST",
                    url: "/api/change_recotd_status.php",
                    data:"id="+id,

                    success: function(data){
                        var result=$.trim(data);
                        if(result="1")
                        {

document.getElementById(id).style.backgroundColor="#009688";
document.getElementById(id).innerHTML ="正常";
//document.getElementById(id).style.display="none";


                            alert("状态变更成功");

                        };
                    }
                });


        }



    </script>




<!---->
<!--                    <div class="layui-form-item">-->
<!--                        <div class="layui-input-block">-->
<!--                            <button class="layui-btn" lay-submit lay-filter="formrentalAdd" type="submit">立即提交</button>-->
<!---->
<!--                        </div>-->
<!--                    </div>-->
                </form>


<!--                <div class="layui-form-item">-->
<!--                    <div class="layui-input-block">-->
<!--                        <a class="layui-btn"  href="#" onClick="javascript:history.go(-2)" >关闭窗口</a>-->
<!--                    </div>-->
<!--                </div>-->

            </div>




            <!--content端點01-->
        </div>
        <!--maincontent端點01-->
        <br class="clearfloat"/>
    </div>
    <!--container端點01-->
</div>





</body>
</html>
